package com.example.dormitorymanagement.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dormitorymanagement.entity.Classmanagement;
import com.example.dormitorymanagement.entity.Service;
import com.example.dormitorymanagement.entity.Visit;
import com.example.dormitorymanagement.exception.AbnormalPermissions;
import com.example.dormitorymanagement.mapper.VisitMapper;
import com.example.dormitorymanagement.service.impl.VisitServiceImpl;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-06-05
 */
//@CrossOrigin
@RestController
@RequestMapping("/visit")
@Api(tags = "访问管理，权限低的可以进行访问的添加，查询，权限高的可以进行添加，查询，删除")
public class VisitController {

    @Autowired
    private VisitServiceImpl visitServiceimpl;

    @Autowired
    private VisitMapper visitMapper;

    @Autowired
    private com.example.dormitorymanagement.service.impl.suthorityservice suthorityservice;

    @PostMapping(value = "/add")
    @ApiOperation(value = "访问的添加")
    public Map<String,Object> add(@RequestBody Visit visit, HttpServletRequest request) throws AbnormalPermissions {

       Map<String, Object> map = suthorityservice.verification(request);

        if (map.size() != 0)
        {
            return map;
        }

        visit.setvisittime(LocalDateTime.now());

//        if(visit.getId() != null && visit.getId() != 0 )//id不为null就代表是修改操作
//        {
//            UpdateWrapper<Visit> visitUpdateWrapper = new UpdateWrapper<>();
//
//            visitUpdateWrapper
//                    .set("visitorname",visit.getVisitorname())
//                    .set("visitorphone",visit.getVisitorphone())
//                    .set("visitfloor",visit.getVisitfloor())
//                    .set("visitthedormitory",visit.getVisitthedormitory());
//
//
//            if (visitServiceimpl.update(visitUpdateWrapper)) {
//
//                map.put("stat",true);
//                map.put("message","更新成功");
//
//            }else {
//
//                map.put("stat",false);
//                map.put("message","更新失败");
//
//            }
//
//            return map;
//
//        }

        boolean save = visitServiceimpl.save(visit);

        if (save) {

            map.put("stat", true);

            map.put("message", "添加成功");

            return map;
        }

        map.put("stat", false);

        map.put("message", "已存在该班级");

        return map;
    }

    @GetMapping(value = "/deletevisit")
    @ApiOperation(value = "通过id来删除对应的访问数据")
    public Map<String,Object> deletevisit(@ApiParam(name = "id",value = "通过id删除对应的数据",required = true) @RequestParam(value = "id") Integer id,HttpServletRequest request) throws AbnormalPermissions {

        Map<String, Object> map = suthorityservice.verification(request);

        if (map.size() != 0)
        {
            return map;
        }

        UpdateWrapper<Visit> visitUpdateWrapper = new UpdateWrapper<>();

        visitUpdateWrapper.eq("id",id);

        int delete = visitMapper.delete(visitUpdateWrapper);

        if (delete == 1)
        {
            map.put("stat",true);

            map.put("message","删除成功");

            return map;
        }

        map.put("stat",false);

        map.put("message","删除失败");

        return map;

    }

    @GetMapping(value = "/getvisit")
    @ApiOperation(value = "通过条件来获取访问表中的所有数据，一页10条数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "visitorname",value = "联合查询条件之一，访问人的名字",dataType = "String"),
            @ApiImplicitParam(name = "pc",value = "第几页，不传值默认为第一页",dataType = "int"),
            @ApiImplicitParam(name = "visitorphone",value = "联合查询条件之一，访问人的电话",dataType = "String"),
    })
    public Map<String, Object> getvisit(String visitorname, Integer pc,String visitorphone)
    {

        Map<String, Object> map = new HashMap<>();

        if (pc == null)
        {
            pc = 1;
        }

        QueryWrapper<Visit> visitQueryWrapper = new QueryWrapper<>();


        map.put("visitorname",visitorname);
        map.put("visitorphone",visitorphone);

        visitQueryWrapper.allEq(map,false).orderByAsc("visittime");

        map.clear();

        Page<Visit> visitPage = new Page<>(pc,10);

        visitMapper.selectPage(visitPage,visitQueryWrapper);

        if ( pc > visitPage.getPages())
        {
            map.put("stat",false);

            map.put("message","页数越界，总页数：" + visitPage.getPages() + "，当前页数:" + visitPage.getCurrent());

            return map;
        }

        List<Visit> visits = visitPage.getRecords();

        map.put("stat",true);
        map.put("visits", visits);
        map.put("pages",visitPage.getPages());
        map.put("page", visitPage.getCurrent());
        map.put("total", visitPage.getTotal());
        map.put("current", visitPage.getCurrent());
        map.put("hasPrevious",visitPage.hasPrevious());
        map.put("hasNext",visitPage.hasNext());

        return map;

    }



}




















